﻿using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace Nantianmen.EntityFrameworkCore.ValueConveters
{
    public class MetadataValueConverter : ValueConverter<Dictionary<string, string>?, string?>
    {
        private const JsonSerializerOptions? nullOptions = null;
        public MetadataValueConverter() : this(
            value => value == null ? null : JsonSerializer.Serialize(value, nullOptions),
            text => string.IsNullOrWhiteSpace(text) ? null : JsonSerializer.Deserialize<Dictionary<string, string>>(text, nullOptions))
        {

        }

        public MetadataValueConverter(
            Expression<Func<Dictionary<string, string>?, string?>> convertToProviderExpression,
            Expression<Func<string?, Dictionary<string, string>?>> convertFromProviderExpression,
            ConverterMappingHints? mappingHints = null) : base(convertToProviderExpression, convertFromProviderExpression, mappingHints)
        {
        }
    }
}
